<template>
  <ele-page>
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
        :where="defaultWhere"
      >
        <template #buy="{ row }">
          <span class="pointer blue" @click="openPay(row)">查看</span>
        </template>
        <template #status="{ row }">
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>
        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
        <template #action="{ row }">
          <el-button-group v-if="status == 0">
            <el-button type="success" size="small" @click="openJudge(row)">
              已收款
            </el-button>
            <el-button type="danger" size="small" @click="judge(row, 30)">
              未收到款
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 图片 -->
    <Images v-model="showImages" :data="current" />
    <Judge v-model="showJudge" :data="current" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import { pageBillRepayRecord, judgeBillRepayRecord } from '@/api/company';
  import Images from './components/images.vue';
  import Search from './components/search.vue';
  import Judge from './components/judge.vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: '编号'
      },
      {
        width: 120,
        slot: 'status',
        label: '状态'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '车牌'
      },
      {
        width: 120,
        prop: 'bill_cate',
        label: '账单类型'
      },
      {
        width: 120,
        prop: 'amount',
        label: '还款金额'
      },
      {
        width: 170,
        prop: 'pay_time',
        label: '还款时间'
      },
      {
        width: 100,
        slot: 'img',
        label: '付款凭证',
        align: 'center'
      },
      {
        width: 120,
        prop: 'create_user',
        label: '还款提交人'
      },
      {
        width: 140,
        prop: 'create_time',
        label: '提交时间'
      },
      {
        slot: 'action',
        label: '操作'
      }
    );
    return cols;
  });

  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageBillRepayRecord({
      ...where,
      ...orders,
      page,
      limit
    });
    return result;
  };

  const status = ref(0);
  /** 搜索 */
  const reload = (where) => {
    status.value = where ? where.status : 0;
    tableRef.value?.reload?.({ page: 1, where });
  };

  const defaultWhere = ref({
    status: 0
  });

  const judge = (row, status) => {
    ElMessageBox.confirm(
      '确定已收到' + row.plate_number + '的' + row.amount + '元还款吗？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        judgeBillRepayRecord({ id: row.id, status })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  const current = ref([]);

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  /** 查看图片 */
  const showJudge = ref(false);
  const openJudge = (data) => {
    current.value = data;
    showJudge.value = true;
  };
</script>
